package com.ch.dream.logic.longstory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.LongStoryContentActivity;
import com.ch.dream.activity.longstory.StoryRecommendActivity;
import com.ch.dream.asynctask.GetStoryRecommendRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.LongstoryBean;
import com.ch.dream.tools.ViewTools;

public class StoryRecommendLogic extends BaseLogic {
	
	private ListView lvStoryRecommend;
	private SimpleAdapter adapterRecommendStory;
	private ProgressDialog loadingDialog;
	private List<HashMap<String, Object>> list; 
	
	public StoryRecommendLogic(StoryRecommendActivity activity) {
		super(activity);
	}

	public StoryRecommendLogic(StoryRecommendActivity activity, int layoutId) {
		super(activity, layoutId);
	}
	
	public void initViews(){
		lvStoryRecommend =(ListView) activity.findViewById(R.id.lvStoryRecommend);
	}
	
	public void init(){
		initViews();
		initStoryRecommendList();
	}
	/**
	 * 初始化推荐故事列表
	 */
	public void initStoryRecommendList(){
		new GetStoryRecommendRequestAsyncTask(activity, new RequestServerListener() {
			@Override
			public void onRequestServerEnd(Object result) {
				loadingDialog.dismiss();
				if(result !=null && !result.toString().equals("[]")){
					JSONObject datas = (JSONObject)result;
					list =  getMystoryList(datas);
					adapterRecommendStory = new SimpleAdapter(activity,list,R.layout.l_mystory_item,
							new String[]{"summary","createTime","shareNum","collectNum","passNum"},new int[]{R.id.tvSummary,R.id.tvCreateTime,R.id.tvShareNum,R.id.tvCollectNum,R.id.tvPassNum}){
						@Override
						public View getView(int position, View convertView,
								ViewGroup parent) {
							convertView = super.getView(position, convertView, parent);
							if(convertView!=null){
								TextView tvShareNum = (TextView)convertView.findViewById(R.id.tvShareNum);
								TextView tvCollectNum = (TextView)convertView.findViewById(R.id.tvCollectNum);
								TextView tvPassNum = (TextView)convertView.findViewById(R.id.tvPassNum);
								TextView tvCreateTime = (TextView)convertView.findViewById(R.id.tvCreateTime);
								tvShareNum.setText("共享"+list.get(position).get("shareNum")+"次");
								tvCollectNum.setText("收藏"+list.get(position).get("collectNum")+"次");
								tvPassNum.setText("传递"+list.get(position).get("passNum")+"次");
								tvCreateTime.setText("始于"+list.get(position).get("createTime").toString().split(" ")[0]);
							}
							return convertView;
						}
					};
					lvStoryRecommend.setAdapter(adapterRecommendStory);
					lvStoryRecommend.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> data, View view,final int position,
								long id) {
							Intent intent =new Intent(activity,LongStoryContentActivity.class);
							intent.putExtra("longStoryId", list.get(position).get("id").toString());
							activity.startActivity(intent);
						};
					});
				}else if(result!=null){
					ViewTools.showShortToast(activity, "暂无推荐");
				}else{
					ViewTools.showShortToast(activity, "网络错误");
				}
			}
			
			@Override
			public void onRequestServerBegin() {
				loadingDialog = ViewTools.showLoading(activity,"提示", "加载数据中，请稍候...");
			}
		}).execute();
	}
	
	/**
	 * 获取推荐数据列表
	 * @param jsonArray
	 * @return
	 */
	public static  List<HashMap<String, Object>> getMystoryList(JSONObject datas){
		List<HashMap<String, Object>> result= new ArrayList<HashMap<String,Object>>();
		
		try{
			JSONArray jsonArray = datas.getJSONArray("longStory");
			for(int i=0;i<jsonArray.length();i++){
				HashMap<String,Object> map = new HashMap<String, Object>();
				LongstoryBean bean = new LongstoryBean();
				bean.setJsonObject(jsonArray.getJSONObject(i));
				map.put("id",bean.getId());
				map.put("ownerId", bean.getOwnerId());
				map.put("creatorId", bean.getCreatorId());
				map.put("collectNum", bean.getCollectNum());
				map.put("shareNum",bean.getShareNum());
				map.put("summary",bean.getSummary());
				map.put("content",bean.getContent());
				map.put("createTime",bean.getCreateTime());
				map.put("deleteFlag",bean.getDeleteFlag());
				map.put("lastModifyTime",bean.getLastModifyTime());
				if(datas.has(bean.getId().toString())){
					map.put("passNum",datas.getJSONObject(bean.getId().toString()).getString("count(*)"));
				}else{
					map.put("passNum","0");
				}
				result.add(map);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
}
